package com.scqkzqtz.base.utils;


import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;
import com.scqkzqtz.base.library.utils.LogUtils;
import com.scqkzqtz.base.model.DBOperationSite;
import com.scqkzqtz.base.model.OperationSiteEntity;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import io.realm.Realm;

/**
 * 运营位工具
 * Created by Administrator on 2017/3/22.
 */

public class OperationSiteUtils {

    public interface OperationSiteListener {
        void onResult();
    }

    /**
     * 获取运营位数据
     */
    public static void getOperationSite() {
        AVQuery<AVObject> query = AVQuery.getQuery("A_DxtOperationSite");
        query.whereEqualTo("isDisable", 0);
        query.whereEqualTo("platform", "app");
        query.orderByDescending("updatedAt");
        query.limit(1000);
        query.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> list, AVException e) {
                if (e == null && list != null && list.size() > 0) {
                    initOperationSiteData(list);
                } else {
                    LogUtils.e(e.getMessage());
                }
            }
        });
    }

    /**
     * 缓存运营数据
     *
     * @param operationSitesLists
     */
    private static void initOperationSiteData(final List<AVObject> operationSitesLists) {
        Realm realm = Realm.getDefaultInstance();
        realm.executeTransactionAsync(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                if (operationSitesLists.size() > 0) {
                    realm.delete(DBOperationSite.class);
                    for (int i = 0, max = operationSitesLists.size(); i < max; i++) {
                        DBOperationSite dBOperationSite = realm.createObject(DBOperationSite.class);
                        AVObject avObject = operationSitesLists.get(i);
                        dBOperationSite.setTitle(avObject.getString("title"));
                        dBOperationSite.setThumbnail(avObject.getString("thumbnail"));
                        dBOperationSite.setUrl(avObject.getString("url"));
                        dBOperationSite.setSn(avObject.getString("sn"));
                        dBOperationSite.setObjectId(avObject.getObjectId());
                        dBOperationSite.setDesc(avObject.getString("desc"));
//                        dBOperationSite.setType(avObject.getString("pageType"));
                        if (avObject.get("version") instanceof Double) {
                            dBOperationSite.setVersion(new BigDecimal(avObject.getDouble("version")).intValue());
                        } else if (avObject.get("version") instanceof Integer) {
                            dBOperationSite.setVersion(avObject.getInt("version"));
                        }
                    }
                }
            }
        });

    }

    public interface HttpListener {
        void succeed(List<OperationSiteEntity> list, String[] images);
    }

    /**
     * 查询运营位
     */
    public static List<DBOperationSite> queryOperationSite(String s, HttpListener httpListener) {
        Realm realm = Realm.getDefaultInstance();
        List<DBOperationSite> list = new ArrayList<>();
        list.addAll(realm.where(DBOperationSite.class).contains("sn", s).findAll());
        List<OperationSiteEntity> OperationSiteData = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            OperationSiteEntity operationSiteEntity = new OperationSiteEntity();
            operationSiteEntity.setDbOperationSite(list.get(i));
            operationSiteEntity.setNumber(list.get(i).getSn().substring(list.get(i).getSn().length() - 1, list.get(i).getSn().length()));
            OperationSiteData.add(operationSiteEntity);
        }
        Collections.sort(OperationSiteData, new MapComparator());
        String[] images = new String[list.size()];
        for (int i = 0; i < OperationSiteData.size(); i++) {
            images[i] = list.get(i).getThumbnail();
        }

        if(httpListener!=null)
            httpListener.succeed(OperationSiteData, images);
        return list;
    }

    /**
     * @param s
     */
    public static List<DBOperationSite> queryOperationList(String s) {
        Realm realm = Realm.getDefaultInstance();
        List<DBOperationSite> list01 = new ArrayList<>();
        List<DBOperationSite> list02 = new ArrayList<>();
        List<DBOperationSite> list03 = new ArrayList<>();
        List<DBOperationSite> list04 = new ArrayList<>();
        List<DBOperationSite> list05 = new ArrayList<>();

        list01.addAll(realm.where(DBOperationSite.class).equalTo("sn", s + "_1").findAll());
        list02.addAll(realm.where(DBOperationSite.class).equalTo("sn", s + "_2").findAll());
        list03.addAll(realm.where(DBOperationSite.class).equalTo("sn", s + "_3").findAll());
        list04.addAll(realm.where(DBOperationSite.class).equalTo("sn", s + "_4").findAll());
        list05.addAll(realm.where(DBOperationSite.class).equalTo("sn", s + "_5").findAll());

        List<DBOperationSite> list = new ArrayList<>();
        if (list01.size() > 0) {
            list.add(list01.get(0));
        }
        if (list02.size() > 0) {
            list.add(list02.get(0));
        }
        if (list03.size() > 0) {
            list.add(list03.get(0));
        }
        if (list04.size() > 0) {
            list.add(list04.get(0));
        }
        if (list05.size() > 0) {
            list.add(list05.get(0));
        }
        return list;
    }

    static class MapComparator implements Comparator<OperationSiteEntity> {

        public int compare(OperationSiteEntity lhs, OperationSiteEntity rhs) {
            return lhs.getNumber().compareTo(rhs.getNumber());
        }

    }
}
